package com.lt.test.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.lt.test.api.KtJoinService;
import com.lt.test.common.exception.CustomException;
import com.lt.test.common.util.StringUtils;
import com.lt.test.entity.DTO.PageDTO;
import com.lt.test.provider.mapper.KtJoinMapper;
import net.sf.json.JSONObject;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(version = "1.0.0")
public class KtJoinServiceImpl implements KtJoinService {
	
	@Resource
	KtJoinMapper joinMapper;
	
	
	/**
	 * 加盟申请
	 * @throws CustomException 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void joinApplay(Map<String, String> parameter) throws CustomException {
		
		String baseInfo = parameter.get("baseInfo");
		String attention = parameter.get("attention");
		String shopSituation = parameter.get("shopSituation");
		Map<String, String> baseInfoMap = JSONObject.fromObject(baseInfo);
		Map<String, String> attentionMap = JSONObject.fromObject(attention);
		baseInfoMap.putAll(attentionMap);
		
		
		String name = baseInfoMap.get("name");
		String idCardNo = baseInfoMap.get("idCardNo");
		List<Map<String, Object>> idNo = joinMapper.getphoneNumber(idCardNo);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dt = format.format(new Date());
		String uuid = StringUtils.getUUID();
		baseInfoMap.put("sign", name);
		baseInfoMap.put("signTime", dt);
		baseInfoMap.put("applayTime", dt);
		baseInfoMap.put("applayNumber", uuid);
		baseInfoMap.put("applayStatus", "0");
		if(shopSituation.equals("") || shopSituation.equals(null)){
			baseInfoMap.put("ownShop", "0");
		}else{
			baseInfoMap.put("ownShop", "1");
		}
		
		if(idNo.isEmpty() && idNo.size()==0){
			joinMapper.baseInfoApplay(baseInfoMap);
			if(!shopSituation.equals("") && !shopSituation.equals(null)){
				Map<String, String> shopSituationMap = JSONObject.fromObject(shopSituation);
				shopSituationMap.put("applayTime", dt);
				shopSituationMap.put("applayNumber", uuid);
				shopSituationMap.put("applayStatus", "0");
				joinMapper.shopApplay(shopSituationMap);
			}
		}else{
			throw new CustomException("该身份证号已注册");
		}	
	}

	/**
	 * 加盟审核
	 */
	@Override
	public void examApplay(Map<String, String> parameter) {

		if(!parameter.isEmpty()){
			//专卖部门意见
			joinMapper.monopolyOpinion(parameter);
			//市州营销部门审核 
			joinMapper.marketingOpinion(parameter);
			//商贸公司意见
			joinMapper.businessCompanyOpinion(parameter);
		}
	}

	/**
	 * 获取加盟申请信息
	 */
	@Override
	public PageDTO<Map<String, Object>> getApplayInfo(Integer page, Integer pageSize,Map<String, String> parameter) {
		List<Map<String,Object>> applayInfo = null;
		applayInfo = joinMapper.getApplayInfo(parameter);
		Integer baseInfoCount = Integer.valueOf(joinMapper.getBaseInfoCount());
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, applayInfo, baseInfoCount);
		return result;
	}

}



